package models

import (
	"iris_test/models"
	"strings"

	"iris_test/Tools"
	"gopkg.in/mgo.v2/bson"
)

type SiteDomain struct {
	models.Models
}

func (siteDomain *SiteDomain) Init() *SiteDomain{
	siteDomain.Dbname = "supsite"
	siteDomain.Tbname = "site_domain"
	siteDomain.InitConn()
	return siteDomain
}

//根据域名获取下对应的站点信息
func (siteDomain *SiteDomain) GetSiteByDomain(domain string) bson.M {
	var subDomain = Tools.SubDomain(domain)
	//存在子域名
	if subDomain != "" {
		if strings.HasPrefix(subDomain, "m.") {
			subDomain = subDomain[strings.Index(subDomain, "m."):]
		}
		siteSet := new(SiteSet)
		res := siteSet.Init().FindOne(bson.M{
			"hostname" : subDomain,
			"state" : bson.M{"$in":[]int{1, -3}},
		})
		siteSet.Close()
		return *res
	}
	return *new(bson.M)
}


